﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using ETWebApi2.Models;
using BllFactory;
using IBLL;

namespace ETWebApi2.Controllers
{
    
    [Route("customerinform"), JwtAuthActionFilter]
    public class CustomerInformController : ApiController
    {
        private Iarticle_tService m_atr = ServiceFactory.CreatService<Iarticle_tService>();
        private ICUser_tbService m_cuser = ServiceFactory.CreatService<ICUser_tbService>();
        [HttpGet]
        public HttpResponseMessage GetbyPage(int page, int limit)
        {
            JwtAuthObject info = new BaseClass().GetInfoByToken(Request.Headers.Authorization.Parameter);
            CUser_tb currentUser = m_cuser.GetList(u => u.Id == info.UserId).FirstOrDefault();
            int count = 0;
            List<article_t> arts = m_atr.GetListByPage(a => a.id > currentUser.ReadID, a => a.id, false, page, limit, ref count);
            return ObjectToJson.toJson(new { status=200,data=new {total = count, rows = arts } });
        }

        /// <summary>
        /// 修改当前用户的readId
        /// </summary>
        [HttpPut]
        public HttpResponseMessage Put()
        {
            JwtAuthObject info = new BaseClass().GetInfoByToken(Request.Headers.Authorization.Parameter);
            article_t atrs = m_atr.GetList(a=>a.art_type==5).OrderByDescending(a=>a.id).FirstOrDefault();
            CUser_tb user = m_cuser.GetList(c=>c.Id==info.UserId).FirstOrDefault();
            user.ReadID = atrs.id;
            if (m_cuser.Update(user))
            {
                return ObjectToJson.toJson(new { status=200,rel=true});
            }
            else
            {
                return ObjectToJson.toJson(new { status = 201, error = "数据出错" });
            }
        }
    }
}
